package com.amigo.online.common.base.exception;

import com.amigo.online.common.core.response.Meta;
import com.amigo.online.common.core.response.StatusCode;
import com.amigo.online.common.util.StringUtils;

/**
 * @author kml
 */
@SuppressWarnings("serial")
public abstract class BaseException extends RuntimeException {

	protected StatusCode statusCode;

	public BaseException() {
	}

	public BaseException(Throwable ex) {
		super(ex);
	}

	public BaseException(String message) {
		super(message);
	}
	public BaseException(StatusCode statusCode) {
		this.statusCode = statusCode;
	}
	public BaseException(StatusCode statusCode, String message) {
		super(message);
		this.statusCode = statusCode;
	}

	public BaseException(String message, Throwable ex) {
		super(message, ex);
	}

	public void handler(Meta meta) {
		meta.setSuccess(false);
		meta.setCode(getStatusCode().value());
		if (StringUtils.isNotBlank(getMessage())) {
			// 取系统的错误消息
			meta.setMsg(getMessage());
		}else {
			meta.setMsg(getStatusCode().msg());
		}
	}

	protected abstract StatusCode getStatusCode();
}
